from warnings import filterwarnings

import pymysql

from apis.db_setting import *

filterwarnings("error", category=pymysql.Warning)


class SqlManager:
    def __init__(self, sentence):
        self.sentence = sentence

    def query(self):
        connection = pymysql.connect(host=HOST, port=PORT, database=DATABASE,
                                     user=USER, password=PASSWORD, charset=CHARSET)
        try:
            with connection.cursor() as cursor:
                cursor.execute(self.sentence)
                result = cursor.fetchall()
                return result
        except Exception as e:
            return e
        finally:
            cursor.close()
            connection.close()

    def modify(self):
        connection = pymysql.connect(host=HOST, port=PORT, database=DATABASE,
                                     user=USER, password=PASSWORD, charset=CHARSET)
        try:
            with connection.cursor() as cursor:
                cursor.execute(self.sentence)
                connection.commit()
        except (pymysql.Warning, Exception) as e:
            return e
        finally:
            connection.close()
